<template>
  <uni-nav-bar :title="cart_code" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package space-between">
    <view>加注机</view>
    <view class="center">
      <hpy-form-select :dataList="array" text="name" name="value" @change="consolu" :Text="FillName" state="admin"
        :icon="true" />
    </view>
  </view>
  <view class="package">
    <view class="datas space-between">
      <uni-datetime-picker v-model="range" type="daterange" :border="false" :clear-icon="false" rangeSeparator="至"
        @change="change" />
      <!--   <picker @change="bindPickerChange" :value="index" :range="array">
        <view class="uni-input">{{array[index]?array[index]:'筛选'}}</view>
      </picker> -->
    </view>
    <view class="center" style="font-size: 26rpx;margin-left:15rpx;">
      <view>收益：{{shouyi}}</view>
      <!--    <view v-if="orderName" class="order">
        <text>{{ orderName }}</text>
        <uni-icons type="closeempty" color="#666" size="18" style="margin-left: 10rpx" @click="del()"></uni-icons>
      </view> -->
    </view>
  </view>
  <!-- 订单列表 -->
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <IncomeOrder :attendanList="attendanList"></IncomeOrder>
    <Popu v-if="attendanList.length<1" :pageHeight="pageHeight" text="暂无订单记录"></Popu>
  </scroll-view>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { format, subDays } from 'date-fns';
  import { ref } from 'vue';
  import { toPublish } from '@mqtt';
  import IncomeOrder from '@c/order/incomeOrder.vue';
  import { onLoad } from '@dcloudio/uni-app';
  import { showLoading, errorToast, successToast } from '@/utils/prompt';
  import { getPatnerCartOrder, getFillList, getPatnerCartInfo } from '@/gql/administrator';
  const index = ref(0)
  const orderName = ref()
  const array = ref([]);
  // data
  const now = new Date();
  let end = format(now, 'yyyy-MM-dd');
  const daysAgo = subDays(now, 6);
  let start = format(daysAgo, 'yyyy-MM-dd');
  const range = ref([start, end]);
  const FillName = ref(''); //加注机名称
  const FillId = ref()

  const attendanList = ref([])
  const lastPage = ref(1);
  const currentPage = ref(1);
  const pageHeight = ref()
  const cart_code = ref()
  const cart_id = ref()
  const shouyi = ref()
  const staff_id = ref()

  const carInfors = ref()
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 200;
    },
  });
  onLoad((pearm) => {
    staff_id.value = Number(pearm.id)
    cart_code.value = pearm.cart_code
    cart_id.value = pearm.cart_id
    init()
    carInfor()
  })
  function change([sta, en]) {
    currentPage.value = 1;
    attendanList.value = [];
    start = sta;
    end = en;
    init();
  }
  function init() {
    showLoading()
    const payload = {
      query: getPatnerCartOrder,
      variables: {
        page: currentPage.value,
        staff_id: staff_id.value,
        start,
        end,
        cart_id: Number(cart_id.value),
      },
    };
    toPublish(
      'ql/staff/getPatnerCartOrder',
      payload,
      (obj : any) => {
        const { getPatnerCartOrder } = obj.data;
        attendanList.value = [...attendanList.value, ...getPatnerCartOrder.list];
        lastPage.value = getPatnerCartOrder.lastpage;
        shouyi.value = getPatnerCartOrder.shouyi
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  //加注机列表
  FillAll()
  function FillAll() {
    showLoading('正在加载');
    const payload = {
      query: getFillList
    };
    toPublish('ql/control/getFillList', payload, (obj : any) => {
      uni.hideLoading();
      const { data } = obj;
      array.value = data.getFillList;
    });
  }
  // function del() {
  //   orderName.value = ''
  //   index.value = 10
  // }
  function carInfor() {
    const payload = {
      query: getPatnerCartInfo,
      variables: {
        cart_id: Number(cart_id.value),
      },
    };
    toPublish('ql/control/getPatnerCartInfo', payload, (obj : any) => {
      uni.hideLoading();
      const { getPatnerCartInfo } = obj.data;
      carInfors.value = getPatnerCartInfo;
      FillName.value = getPatnerCartInfo.fill_name
    });
  }
  //切换加注机ID
  function consolu(e : any) {
    console.log(JSON.stringify(e) + '--------')
    // FillName.value = e.data.name;
    // FillId.value = e.data.id;
    showLoading()
    const payload = {
      cart_id: Number(cart_id.value),
      fill_id: e.data.id,
    };
    toPublish(
      'control/editPatnerCartFill',
      payload,
      (obj : any) => {
        const { code, msg } = obj;
        close()
        if (code === 1) {
          successToast(msg)
          setTimeout(() => {
            carInfor()
          }, 600)
        } else {
          errorToast(msg)
        }
      }
    );
  }
</script>

<style scoped lang="less">
  .order {
    display: flex;
    align-items: center;
    padding: 2px 20rpx;
    box-sizing: border-box;
    margin-left: 20rpx;
    background-color: #eee;
    border-radius: 30rpx;
  }
</style>
<style>
  .datas>>>.uni-date__x-input {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-date-x {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-icons {
    font-size: 48rpx !important;
  }

  .center>>>.select-picker {
    justify-content: flex-end;
  }

  .center>>>.select-picker text {
    text-align: right;
  }
</style>